import Base from "./base";

import { MiniApp } from "node-easywechat";
// 实例化应用

export default class extends Base {
  /**
   * 公众号登录
   */
  async offiAction() {}
  loginAction() {
    console.log("这是到了那里");
  }
  async miniAction() {
    const code = this.post("code");
    console.log(code);
    const weixinMiniConfig = this.config("weixinMini") || [];
    const wechatService = think.service("wechat", "api");
    const $session = await wechatService.getOpenid(code, weixinMiniConfig);
    console.log("获取的内容");
    console.log($session);
    if (think.isEmpty($session) || think.isEmpty($session.openid)) {
      return this.fail(403, "微信登录失败" + $session.errmsg);
    }
    const { openid, unionid } = $session;
    const memberService = think.service("memberService");
    const user = {
      nickname: "微信用户",
      openid,
      unionid,
      login_ip: this.ctx.ip,
      reg_channel: 3,
      reg_type: 5,
    };
    const userInfo = await memberService.wechat(user);
    console.log('什么鬼啊啊');
    
    console.log(userInfo);
    const TokenSerivce = this.service("token", "admin");
    const sessionKey = await TokenSerivce.create(userInfo.member_id);
    if (think.isEmpty(sessionKey)) {
      return this.fail("登录失败");
    }

    return this.success({ token: sessionKey, member_id: userInfo.id });
  }
}
